package com.example.spring_java_oj.service;

import com.example.spring_java_oj.Util.ImageUtil;
import com.example.spring_java_oj.Util.LoginUtil;
import com.example.spring_java_oj.mapper.UserInfoMapper;
import com.example.spring_java_oj.model.UserInfo;
import com.example.spring_java_oj.service.userInfo.UserInfoService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.ByteArrayInputStream;
import java.util.concurrent.TimeUnit;

@Service
public class UserLoginService {

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;



    //验证登录
    public String login(String username, String password, String captcha, HttpServletRequest request){
        if (!StringUtils.hasLength(username) || !StringUtils.hasLength(password)){
            return "用户名或密码为空";
        }
        //获取数据库中的密码
        UserInfo userInfo = userInfoService.queryByName(username);
        if (userInfo==null || userInfo.getId()<0){
            return "用户不存在";
        }
//        if (!password.equals(userInfo.getPassword())){
//            return Result.fail("密码错误!");
//        }
        if (!password.equals(userInfo.getPassword())){
            return "密码错误!";
        }

        if(!LoginUtil.isTrue(captcha, request, stringRedisTemplate)){
            return "验证码错误";
        }

        //生成token, 并返回
//        Map<String, Object> claim = new HashMap<>();
//        claim.put("id", userInfo.getId());
//        claim.put("name", userInfo.getUserName());
//        claim.put("manager", userInfo.getManager());
//        String token = JwtUtils.genToken(claim);
        HttpSession session = request.getSession();
        session.setAttribute("id", userInfo.getId());
        session.setAttribute("username", userInfo.getUserName());
        session.setAttribute("password", userInfo.getPassword());
        //session.removeAttribute("verifyCode");
        stringRedisTemplate.opsForValue().decrement("verifyCode");
        return null;
    }

    @SneakyThrows
    public void buildCheckImage(HttpServletRequest request, HttpServletResponse response) {
        final ImageUtil imageUtil = ImageUtil.getInstance();
        final ByteArrayInputStream image = imageUtil.getImage();
        //验证码图片
        final String verifyCode = imageUtil.getStr();
        //验证码文字
//        HttpSession session = request.getSession(true);//如果没有session，则允许创建
//        session.setAttribute("verifyCode",verifyCode);
        stringRedisTemplate.opsForValue().set("verifyCode", verifyCode);
        stringRedisTemplate.expire("verifyCode", 60, TimeUnit.SECONDS);
        response.setContentType("image/jpeg");

        byte[] bytes = new byte[1024];
        try (final ServletOutputStream out = response.getOutputStream()){
            while (image.read(bytes) != -1){
                out.write(bytes);
            }
        }
    }

    //注册
    public boolean regster(String username, String passwordA, String passwordB) {
        if(!passwordA.equals(passwordB)){
            return false;
        }
        int n = userInfoMapper.register(username, passwordA);
        if(n == 1){
            return true;
        }
        return false;
    }

}
